package com.ruoyi.web.web.form;

import com.ruoyi.accountant.exception.ValidateException;
import lombok.Data;
import org.springframework.util.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/***
 * 交易记录列表表单
 */
@Data
public class TransactionDetailListForm implements Validatable {
    private String transactionDate; // 交易日期
    private String proofDate; // 凭证日期
    private String transactionType; // 交易类型
    private Long ofAccount; // 所属账户
    private String other; // 其它
    private Long reason; // 收支原因
    private Integer rows; // 条数
    private Integer page; // 页数
    private boolean transactionDateOn;
    private boolean proofDateOn;
    private Long ofCompany;

    private Date transactionStartDate; // 交易日期开始
    private Date transactionEndDate; // 交易日期结束
    private Date proofStartDate; // 凭证日期开始
    private Date proofEndDate; // 凭证日期结尾

    public Validation validate() {
        try {
            // 分页数
            if(rows == null || rows < 0) {
                rows = 50;
            }
            if(page == null || page < 0) {
                page = 1;
            }

            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM/dd/yyyy");

            // 日期
            if(StringUtils.isEmpty(transactionDate)) {
                Calendar c = Calendar.getInstance();
                c.setTime(new Date());
                c.add(Calendar.MONTH, -1);

                transactionDate = simpleDateFormat.format(c.getTime())+"-"+simpleDateFormat.format(new Date());
            }


            if(StringUtils.isEmpty(proofDate)) {
                Calendar c = Calendar.getInstance();
                c.setTime(new Date());
                c.add(Calendar.MONTH, -1);

                proofDate = simpleDateFormat.format(c.getTime())+"-"+simpleDateFormat.format(new Date());
            }

            if(!StringUtils.isEmpty(transactionDate)) {
                String[] transactionDateValues = transactionDate.split("-");
                this.transactionStartDate = simpleDateFormat.parse(transactionDateValues[0]);
                this.transactionEndDate = simpleDateFormat.parse(transactionDateValues[1]);
            }
            if(!StringUtils.isEmpty(proofDate)) {
                String[] proofDateValues = proofDate.split("-");
                this.proofStartDate = simpleDateFormat.parse(proofDateValues[0]);
                this.proofEndDate = simpleDateFormat.parse(proofDateValues[1]);
            }

            Validation validation = new Validation();
            validation.setOk(true);
            validation.setMessage("test");
            return validation;
        } catch (ParseException e) {
            throw new ValidateException(e);
        }
    }
}
